package com.speedydial;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ListContactPendingActivity extends Activity implements SensorEventListener {
	
	private static final int ACTIVITY_EDIT=1;
    
	/** temp list of contacts */
	private String[] peopleList;
	
	/** Sensor management */
	private SensorManager sensorManager;
	
	private TextView textLabel;
	
	private TextView xAccelLabel;
	
	/** flag that signals the sending of sms */
	private boolean isAlreadySent;
	
	private static final String patriciaMovil = "618832981";
	
	private static final String lateMsg = "Estare llegando 10 minutos tarde";
	
	private static final String superLateMsg = "Estare llegando 20 minutos tarde";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setAlreadySent(false);
        
     // activamos el sensor
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        
        setContentView(R.layout.form_layout);
        
        textLabel = (TextView) findViewById(R.id.display_text);
        
        textLabel.setText("OK");
        
        
        xAccelLabel = (TextView) findViewById(R.id.x_accel_text);
        xAccelLabel.setText("x acel");
  
        Button button = (Button) findViewById(R.id.sms_button);
        
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textLabel.setText("reset");
				setAlreadySent(false);
			}
		});
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	// Register this class as a listener for the accelerometer sensor
    	sensorManager.registerListener(this, 
    			sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
    			SensorManager.SENSOR_DELAY_NORMAL);
    	// ...and the orientation sensor
    	sensorManager.registerListener(this, 
    			sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
    			SensorManager.SENSOR_DELAY_NORMAL);
    }
  
    @Override
    protected void onStop() {
    	// Unregister the listener
    	sensorManager.unregisterListener(this);
    	super.onStop();
    } 


	private void initList() {
		peopleList = new String[]{"Patricia"};
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {

		float[] gravity = new float[3];
		float[] linear_acceleration = new float[3];
		
		synchronized (this) {
		    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
		    	// calculate the x speed
		    	// trying the low pass filter to  get horizontal speed
		    	final float alpha = 0.8f ;

		          gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];

		          linear_acceleration[0] = event.values[0] - gravity[0];

		          if( linear_acceleration[0] > 1 && !isAlreadySent()){
		        	  textLabel.setText(Float.toString( linear_acceleration[0] ));
		          } 
		          
		          xAccelLabel.setText( Float.toString(event.values[0]) );
		          
		          if ( event.values[0] < -4 ){
		        	  // send message 10 minutes late
		        	  //sendMessage(1);
		        	  setAlreadySent(true);
		        	  
		          } else if ( event.values[0] > 4 ){
		        	  // send message 20 minutes late
		        	  //sendMessage(2);
		        	  setAlreadySent(true);
		          }
		    	
		    }
		} // end of sync
		
	} // sensor changed

	
	private void sendMessage(int condition){
		
		if( isAlreadySent() ){
			return;
		}
		
		PendingIntent pi = null;
		
		SmsManager smsMan = SmsManager.getDefault();
		
		switch ( condition) {
		case 1:
			smsMan.sendTextMessage(
    				patriciaMovil, //user.getPhone(), 
    				null,
    				lateMsg, 
    				pi, null);
			break;

		default:
			smsMan.sendTextMessage(
    				patriciaMovil,  
    				null,
    				superLateMsg, 
    				pi, null);
			break;
		}
		
		textLabel.setText("Msg Sent");
	}
	
	public boolean isAlreadySent() {
		return isAlreadySent;
	}

	public void setAlreadySent(boolean isAlreadySent) {
		this.isAlreadySent = isAlreadySent;
	}
	
	

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		Log.i("LIST", "menu: " + item.getItemId() );
		switch(item.getItemId()){
		
		case R.id.add_menu:{
			Log.i("LIST", "menu selected");
			Intent i = new Intent(this, ContactFormActivity.class);
//	        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
	        startActivityForResult(i, ACTIVITY_EDIT);
			break;
		}
		default:{
			Log.v("Contact", "other menu selected");
		}
		}// end switch
		return super.onMenuItemSelected(featureId, item);
	}
	
	

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.speedy_menu, menu);
		return true;
	}
	
	
	
	
}